Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
how do I get only the date from the string "2022-05-25T15:48:50.000Z" using moment.js?

I am using moment.js library and for the example lets say I have the code -

import moment from "moment-timezone";

const date = "2022-05-25T15:48:50.000Z"

I've tried using moment(date).format("YYYY-MM-DD") but I am getting the previous date - 2022-05-24 as a result.

I know it has to do with utc, and my project has in it's setting the line 'moment.tz.setDefault("Etc/UTC")` which from my understand is supposed to fix it, but it doesn't.

I only need it to have the final result of 2022-05-25 and I know I am missing something really small here..

I'd appreciate any help!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

try this var newStringDate = yourDate.toISOString().slice(0, 10); or

var newStringDate = yourDate.toISOString().substring(0, 10);
about 4 years ago · Juan Pablo Isaza Denunciar

0

Your format is ISO8601 format, you could check from here: https://en.wikipedia.org/wiki/ISO_8601

You can try to send the full format to the function

const date = "2022-05-25T15:48:50.000Z"
var now = moment.utc(date,"YYYY-MM-DD\THH:mm:ss\Z").format("YYYY-MM-DD");
document.getElementById('now').innerHTML = now;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>
<strong>Should Show: 2022-05-25</strong>
<div id="now"></div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda